
package com.sskj.jkzx.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.amap.yc.YcMarkerInfo;
import com.sskj.jkzx.domain.entity.amap.yc.ro.EquipmentInfo;
import com.sskj.jkzx.domain.entity.amap.yc.ro.ProjectInfo;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "**/sskj/amap")
@Api(value = "工地扬尘数据查询", description = "工地扬尘数据查询")
public class YcMarkersInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(YcMarkersInfoServiceImpl.class);

	@Value("${sskj.ycServer}")
	private String ycServer;

	@Value("${sskj.amap.city}")
	private String city;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/markers/ycmarkers", method = RequestMethod.GET)
	@ApiOperation(value = "工地扬尘实时数据查询", httpMethod = "GET")
	public List<YcMarkerInfo> queryYcMarkerInfo() {

		List<YcMarkerInfo> result = new ArrayList<YcMarkerInfo>();

		try {
			String resultStr = HttpHelp.sendGet(ycServer + "/ycvideoInterface.do", null);

			JSONObject resultJson = JSONObject.parseObject(resultStr);

			if (resultJson == null) {
				return result;
			}
			List<ProjectInfo> projectInfos = JSON.parseArray(resultJson.getJSONArray("projectInfo").toJSONString(),
					ProjectInfo.class);

			for (ProjectInfo pi : projectInfos) {
				if (pi.getList().size() == 0) {
					continue;
				}
				YcMarkerInfo marker = new YcMarkerInfo();
				marker.setId(pi.getPcode());
				marker.setName(pi.getPname());
				marker.setLng(pi.getLngGd());
				marker.setLat(pi.getLatGd());
				marker.setPm2Standard(pi.getPm2Standard());
				marker.setPm10Standard(pi.getPm10Standard());

				if ("1".equals(pi.getColor())) {
					marker.setAlertColor("green");
				} else if ("2".equals(pi.getColor())) {
					marker.setAlertColor("yellow");
				} else if ("3".equals(pi.getColor())) {
					marker.setAlertColor("red");
				} else if ("4".equals(pi.getColor())) {
					marker.setAlertColor("yellow");
				} else if ("5".equals(pi.getColor())) {
					marker.setAlertColor("red");
				} else {
					marker.setAlertColor("green");
				}

				double pm2 = 0;
				double pm10 = 0;
				for (EquipmentInfo e : pi.getList()) {
					BigDecimal v1 = new BigDecimal(e.getPmTen());
					BigDecimal v2 = new BigDecimal(pm10);
					if (v1.compareTo(v2) >= 0) {
						pm10 = e.getPmTen();
					}
					BigDecimal v3 = new BigDecimal(e.getPmTwo());
					BigDecimal v4 = new BigDecimal(pm2);
					if (v3.compareTo(v4) >= 0) {
						pm2 = e.getPmTwo();
					}
				}
				marker.setPm2(new BigDecimal(pm2).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
				marker.setPm10(new BigDecimal(pm10).setScale(1, BigDecimal.ROUND_HALF_UP).toString());

				result.add(marker);
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		// logger.info(JSONArray.toJSONString(result));

		return result;
	}

}
